<?php
/**
 * Created by PhpStorm.
 * User: xaq
 * Date: 2017/6/29
 * Time: 15:07
 */

namespace app\index\controller;

use app\index\model\UserTable as UserModel;
use app\index\validate\LoginDataValidate;
use app\lib\exception\TelephoneException;
use think\Request;
use think\Session;


class User extends BaseController
{
    public function index()
    {

        return $this->fetch('teacherLogin');
    }

    /**
     * 会员绑定功能
     * 1、如果输入手机号，是会员记录里的，直接获取这条记录，直接进行登陆
     * 如果输入手机号，在会员记录表中不存在，应该插入一条记录，然后进行登陆
     */
    public function login()
    {
        //手机号和验证码的输入验证
        //(new LoginDataValidate())->goCheck();

        $data = input('post.');
        $account = $data['account'];
        $password = $data['password'];

        //登录验证
        $result = UserModel::get([
            'Code' => $account,
            'PassWord' => $password,
        ]);

        if (!$result) {
            $this->error('账号或密码错误');
        }
        //获取机构名称
        $name = $result['Name'];
        Session::set('name', $name);
        //用户姓名
        $code = $result['Code'];
        Session::set('code', $code);
        //设置教师id
        $teachID = $result['ID'];
        Session::set('teachID',$teachID);
        //设置班级信息
        $schoolID = $result['schoolID'];
        Session::set('schoolID',$schoolID);


        if (!captcha_check($data['verifyCode'])) {
            // 校验失败
            $this->error('验证码不正确');
        }

        //跳转到teacherInfo.html页面
        $this->success('登录成功', config('setting.baseUrl') . '/index/call');
    }

    public function actionBind()
    {

        $telephone = input('post.telephone');
        $verifyCode = input('post.verifyCode');
        //验证手机号在数据库里是否有记录
        $memberInfo = UserModel::get([
            'telephone' => $telephone,
            'status' => 1
        ]);
        if (!$memberInfo) {
            //判断手机号是否唯一
            if (UserModel::get(['telephone' => $telephone])) {
                throw new TelephoneException();
            }
            //新增一条记录
            $memberInfo = UserModel::create([
                'Name' => $telephone,
                'telephone' => $telephone,
                'status' => 1
            ]);
        }
    }


}